package com.gzhryc.shared_device.bxscn.core.protocol.v1;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.bxscn.core.BXSCNConstants;
import com.gzhryc.shared_device.bxscn.core.IBXSCNNetworkService;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNExecuteResult;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNLeaseCommand;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNOpenSlotCommand;
import com.gzhryc.shared_device.bxscn.core.protocol.v1.msg.*;
import com.gzhryc.shared_device.iot.core.IOTClient;

import java.util.regex.Matcher;

public class V1_BXSCNNetworkService implements IBXSCNNetworkService {

    static Logger log = Logger.getLogger(V1_BXSCNNetworkService.class);

    private final IOTClient iotClient;

    public V1_BXSCNNetworkService(IOTClient iotClient){
        this.iotClient = iotClient;
    }

    @Override
    public BXSCNExecuteResult lease(BXSCNLeaseCommand leaseCommand) {
        BXSCNExecuteResult result = new BXSCNExecuteResult(BXSCNExecuteResult.FAIL);
        try {
            String cmd = null;
            String content = null;

            if(leaseCommand.getSlotNum() != null){
                FBRequest fbRequest = new FBRequest();
                fbRequest.setTradeNo(leaseCommand.getTradeNo());
                fbRequest.setSlotNum(leaseCommand.getSlotNum());
                cmd = fbRequest.getCmd();
                content = fbRequest.getMessage();
                if(StringTools.isNotBlank(leaseCommand.getMessageId())) {
                    fbRequest.setMessageId(leaseCommand.getMessageId());
                }else{
                    leaseCommand.setMessageId(fbRequest.getMessageId());
                }
            }else {
                BWRequest bwRequest = new BWRequest();
                bwRequest.setTradeNo(leaseCommand.getTradeNo());
                cmd = bwRequest.getCmd();
                content = bwRequest.getMessage();
                if(StringTools.isNotBlank(leaseCommand.getMessageId())) {
                    bwRequest.setMessageId(leaseCommand.getMessageId());
                }else{
                    leaseCommand.setMessageId(bwRequest.getMessageId());
                }
            }

            V1_BXSCN_IOTMessage message = new V1_BXSCN_IOTMessage(content);
            message.setMessageId(leaseCommand.getMessageId());
            message.setDeviceSn(leaseCommand.getDeviceSn());
            message.setCmd(cmd);
            message.setTopic("/" + leaseCommand.getDeviceSn() + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(BXSCNExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public BXSCNExecuteResult openSlot(BXSCNOpenSlotCommand openSlotCommand) {
        BXSCNExecuteResult result = new BXSCNExecuteResult(BXSCNExecuteResult.FAIL);
        try {
            Integer slotNum = openSlotCommand.getSlotNum();
            if(slotNum == null){
                slotNum = 0;    //0代表自动设置
            }

            FRRequest frRequest = new FRRequest();
            frRequest.setSlotNum(slotNum);
            if(StringTools.isNotBlank(openSlotCommand.getMessageId())) {
                frRequest.setMessageId(openSlotCommand.getMessageId());
            }else{
                openSlotCommand.setMessageId(frRequest.getMessageId());
            }

            V1_BXSCN_IOTMessage message = new V1_BXSCN_IOTMessage(frRequest.getMessage());
            message.setMessageId(frRequest.getMessageId());
            message.setDeviceSn(openSlotCommand.getDeviceSn());
            message.setCmd(frRequest.getCmd());
            message.setTopic("/" + openSlotCommand.getDeviceSn() + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(BXSCNExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public BXSCNExecuteResult forceEjectDevice(String deviceSn, Integer slotNum) {
        BXSCNExecuteResult result = new BXSCNExecuteResult(BXSCNExecuteResult.FAIL);
        try {
            FARequest faRequest = new FARequest();
            faRequest.setSlotNum(slotNum);

            V1_BXSCN_IOTMessage message = new V1_BXSCN_IOTMessage(faRequest.getMessage());
            message.setMessageId(faRequest.getMessageId());
            message.setDeviceSn(deviceSn);
            message.setCmd(faRequest.getCmd());
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(BXSCNExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public BXSCNExecuteResult forceEjectDevice(String deviceSn) {
        BXSCNExecuteResult result = new BXSCNExecuteResult(BXSCNExecuteResult.FAIL);
        try {
            FARequest faRequest = new FARequest();
            faRequest.setSlotNum(0);

            V1_BXSCN_IOTMessage message = new V1_BXSCN_IOTMessage(faRequest.getMessage());
            message.setMessageId(faRequest.getMessageId());
            message.setDeviceSn(deviceSn);
            message.setCmd(faRequest.getCmd());
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(BXSCNExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public BXSCNExecuteResult searchDevice(String deviceSn) {
        BXSCNExecuteResult result = new BXSCNExecuteResult(BXSCNExecuteResult.FAIL);
        try {
            CQRequest cqRequest = new CQRequest();
            cqRequest.setSlotNum(0);

            V1_BXSCN_IOTMessage message = new V1_BXSCN_IOTMessage(cqRequest.getMessage());
            message.setMessageId(cqRequest.getMessageId());
            message.setDeviceSn(deviceSn);
            message.setCmd(cqRequest.getCmd());
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(BXSCNExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public BXSCNExecuteResult sendCmd(String deviceSn, String content) {
        BXSCNExecuteResult result = new BXSCNExecuteResult(BXSCNExecuteResult.FAIL);
        try {
            String cmd = null;
            String msgId = null;
            Matcher mat = BXSCNConstants.CmdPattern.matcher(content);
            if (mat.find()) {
                cmd = mat.group(1);
                //消息ID
                String[] data = mat.group(2).split(",");
                msgId = data[0];
            }

            V1_BXSCN_IOTMessage message = new V1_BXSCN_IOTMessage(content);
            message.setMessageId(msgId);
            message.setDeviceSn(deviceSn);
            message.setCmd(cmd);
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(BXSCNExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
